package com.mike.controller.portal;

import com.mike.common.Const;
import com.mike.common.ResponseCode;
import com.mike.common.ServerResponse;
import com.mike.pojo.User;
import com.mike.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by HP on 2018/2/28.
 */
@Controller
@RequestMapping("/user/")
public class UserAction {

    @Autowired

    private IUserService userService;


    @RequestMapping(value = "login.do",method = RequestMethod.POST)
    //使用 @ResponseBody注解，根据在springmvc-dispatcher配置文件通过Jackson插件自动将对象转换成json数据
    @ResponseBody
    public ServerResponse<User> login(String username, String password, HttpSession session){
        ServerResponse<User> responseResult = userService.login(username,password);
        if (responseResult.isSuccess()){
            session.setAttribute(Const.CURRENT_USER,responseResult.getData());
        }
        return  responseResult;
    }


    /**TODO 检查注册时的用户名或者邮箱是否可用
     * param str
     * param type
     * return
     *
     */
    @RequestMapping(value = "check_valid.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> checkValid(String str,String type){
        return userService.checkValid(str,type);
    }



    //注册
    @RequestMapping(value = "register.do",method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> register(User user){
        ServerResponse<String> responseResult = userService.register(user);
        return responseResult;
    }


    /**
     * 用户注销
     * @param session
     * @return
     */
    @RequestMapping(value = "logout.do",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<String> logout(HttpSession session){
        session.removeAttribute(Const.CURRENT_USER);
        return ServerResponse.createSuccessResponse();
    }

    /**
     * 获取已登录的用户信息
     *
     * @param session
     * @return
     */
    @RequestMapping(value = "get_user_info.do",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<User> getUserInfo(HttpSession session){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user != null){
            return ServerResponse.createErrorMessageResponse(user);
        }
        return ServerResponse.createErrorMessageResponse("用户尚未登录，无法获取用户当前信息");
    }

    /**
     * 忘记密码，根据用户名获取提示问题，修改密码
     */
    @RequestMapping(value = "forget_get_question.do",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<String> forgetPasswordGetQuestion(String username){
        return userService.getQuestionByUsername(username);
    }

    /**
     * 验证用户预留答案是否正确
     * 忘记密码，根据提示问题，验证答案
     * (用到了token令牌及guava缓存技术）
     */
    @RequestMapping(value = "forget_check_answer.do",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<String> forgetPasswordCheckAnswer(String username,String question,String answer){

        return userService.checkAnswer(username,question,answer);
    }


    /**
     * 3.6
     * 用户预留答案验证后重置密码
     * param username
     * param passwordNew
     * param forgetToken
     * @return
     */
    @RequestMapping(value = "forget_reset_password.do",method = RequestMethod.GET)
    @ResponseBody
    public ServerResponse<String> forgetResetPassword(String username,String passwordNew,String forgetToken){
       return userService.forgetResetPassword(username,passwordNew,forgetToken);
    }

    /**
     * 在已登录状态下，对密码进行修改
     * param session
     * param passwordOld
     * param passwordNew
     * return
     */
    public ServerResponse<String> resetPassword(HttpSession session,String passwordOld, String passwordNew){
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        if(user == null){
            return ServerResponse.createErrorMessageResponse("用户尚未登录");

        }
        return userService.resetPassword(passwordOld,passwordNew,user);
    }

    /**
     * 已登录用户修改自己的用户信息
     * param user 封装了来自界面的用户更新信息，注意，该user中没有userid
     * param session
     * return
     */
    @RequestMapping(value = "update_information.do",method=RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> updateUserInformation(User user,HttpSession session){
            //验证用户是不已经登录
        User currentUser = (User) session.getAttribute(Const.CURRENT_USER);
        if(currentUser == null){
            return ServerResponse.createErrorMessageResponse("用户尚未登录");
        }
        //更新是，userId及username不允许被修改，因此要从session的当前对象中读取
        user.setId(currentUser.getId());
        user.setUsername(currentUser.getUsername());
        //更新信息，放入session
        ServerResponse<User> resultResponse = userService.updateInformation(user);
        if(resultResponse.isSuccess()){
            session.setAttribute(Const.CURRENT_USER,resultResponse.getData());
        }
        return resultResponse;



    }

    /**
     * 3.9 已登录用户获取用户详细信息
     */
        @RequestMapping(value ="get_information.do",method =RequestMethod.GET)
        @ResponseBody
        public ServerResponse<User> getInformation(HttpSession session){
            User currentUser = (User) session.getAttribute(Const.CURRENT_USER);
            if(currentUser == null){
                return ServerResponse.createErrorCodeMsg(ResponseCode.NEED_LOGIN.getCode(),
                        "用户未登录，返回状态码10，前端判断状态码并实现强制登录");

            }
           return userService.getInformation(currentUser.getId());
        }
}
